#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/31 0031 15:17
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : hkd

import scrapy
import time
import smtplib
import re

from scrapy_redis.spiders import RedisSpider
from notify.items import HkdItem


class HkdSpider(RedisSpider):
    name = 'hkdnotify'
    redis_key = "notify:hkdno"
    custom_settings = {
        "REDIS_PARAMS": {
            'db': 1,
            'password': '5tgbnhy67ujm'
        }
    }
    def __init__(self, *args, **kwargs):
        domain = kwargs.pop('domian', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(HkdSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        _all = response.xpath('//*[starts-with(@id,"line54026_")]')
        item = HkdItem()
        for x in _all:
            item['title'] = x.xpath('td/a/@title')[0].extract()
            uu = re.search(r'.*(/i.*)', x.xpath('td/a/@href')[0].extract()).group(1)
            item['url'] = "http://zjc.haust.edu.cn%s" % uu
            item['ntime'] = x.xpath('td/span/text()')[1].extract().strip()
            if re.match(r'.*zpgg.*', response.url):
                item['type'] = "招聘"
            else:
                item['type'] = "宣讲"
            yield item

            # lpush notify:hkdno http://zjc.haust.edu.cn/index/jycyzdzx/jysy/xyxjh.htm
            # lpush notify:hkdno http://zjc.haust.edu.cn/index/jycyzdzx/jysy/zpgg.htm
